<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * @todo 限制图片上传后缀名和大小
 * @todo 上传图片之后修改图片大小
 * @bug 简介字数多时添加图书失败
 * @remark 我们不生产代码，我们只是代码的搬运工
 */

include ("../conn/conn.php");
$conn = prepare();

/**
 * @brief 生成图片路径名
 * @param [in] $name 图片的后缀名
 * @return 生成的图片路径名
 */
function getname($exname){
   $dir = "uploads/";
   $i=1;
   
   if(!is_dir($dir)){
      mkdir($dir,0777);
   }
   
   while(true){
       if(!is_file($dir.$i.".".$exname)){
	       $name=$i.".".$exname;
	       break;
	   }
	   $i++;
	 }

   return $dir.$name;
}



$mingcheng = $_POST['mingcheng'];
$jinjia = $_POST['jinjia'];
$danjia = $_POST['danjia'];
$leixing = $_POST['leixing'];
$shuliang = $_POST['shuliang'];
$author = $_POST['author'];
$jianjie = nl2br($_POST['jianjie']);///< @remark 修复了文本框中文本换行而存入数据库之后不现实换行的Bug
$mulu = nl2br($_POST['mulu']);

/*
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);        // 获取文件后缀名
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 204800)    // 小于 200 kb
&& in_array($extension, $allowedExts))
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "错误：: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
        echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
        echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"];
    }
}
else
{
    echo "非法的文件格式";
}
*/

$exname=strtolower(substr($_FILES['upfile']['name'],(strrpos($_FILES['upfile']['name'],'.')+1)));//获取图片后缀名
$uploadfile = getname($exname);//生成新图片名

if(trim($_FILES['upfile']['name']!="")) {
    list($width,$height)=getimagesize($_FILES['upfile']['tmp_name']);
    $src = imagecreatefromjpeg($_FILES['upfile']['tmp_name']);
    move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile);
    $newwidth=600;
    $newheight=($height/$width)*600;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    imagejpeg($tmp,$uploadfile,100);
    imagedestroy($src);
    imagedestroy($tmp);

    $uploadfile="admin/".$uploadfile;
} else {
    $uploadfile="";
}

exectue_sql_statement($conn, 
                      "insert into ".
                      "Book(book_name, book_author, book_introduction, book_catalog, unit_price, classify_id, book_img, purchase_price) ".
                      "values('$mingcheng', '$author', '$jianjie', '$mulu', $danjia, $leixing, '$uploadfile', '$jinjia')"
                     );
$stmt = exectue_sql_statement($conn, 
                                 "select book_id from Book where book_name='$mingcheng' order by book_id desc"
                                );//这里使用desc的目的是防止添加图书时书名相同的情况下获得的book_id为最早的值
$info = sqlsrv_fetch_array($stmt);
exectue_sql_statement($conn, 
                      "insert into Store values(".$info['book_id'].", $shuliang)"
                     );
echo "<script>alert('图书添加成功!');window.location.href='addbook.php';</script>";
?>